Branchwell

Lumen & Gesso / website · lumengesso.com / app / orders / [id] / approve / form.tsx

1'use client';
2
3import { useState, type FormEvent } from 'react';
4import { ORDER_MESSAGE_LIMIT } from '@/app/lib/order-messages';
5
6export function ApproveForm({ orderId, generationId }: { orderId: string; generationId: string | null }) {
7 const [termsAccepted, setTermsAccepted] = useState(false);
8 const [mode, setMode] = useState<'idle' | 'approving' | 'revising'>('idle');
9 const [reviseNote, setReviseNote] = useState('');
10 const [error, setError] = useState<string | null>(null);
11 const [showRevise, setShowRevise] = useState(false);
12
13 const onApprove = async (e: FormEvent) => {
14 e.preventDefault();
15 if (!termsAccepted) return;
16 setMode('approving');
17 setError(null);
18 try {
19 const res = await fetch(`/api/orders/${orderId}/approve`, {
20 method: 'POST',
21 headers: { 'content-type': 'application/json' },
22 body: JSON.stringify({ termsAccepted: true, generationId }),
23 });
24 if (!res.ok) {
25 const data = await res.json().catch(() => null) as { error?: string } | null;
26 throw new Error(data?.error ?? `Approve failed (${res.status})`);
27 }
28 // Payment was collected at /checkout up-front, so approving is
29 // purely a confirmation. Send the customer to their order page
30 // where the "approved" status copy explains the studio review
31 // step before printing.
32 window.location.href = `/orders/${orderId}?approved=1`;
33 } catch (e) {
34 setError(e instanceof Error ? e.message : 'Approve failed');
35 setMode('idle');
36 }
37 };
38
39 const onRevise = async (e: FormEvent) => {
40 e.preventDefault();
41 if (!reviseNote.trim()) return;
42 setMode('revising');
43 setError(null);
44 try {
45 const res = await fetch(`/api/orders/${orderId}/revise`, {
46 method: 'POST',
47 headers: { 'content-type': 'application/json' },
48 body: JSON.stringify({ note: reviseNote.trim(), generationId }),
49 });
50 if (!res.ok) {
51 const data = await res.json().catch(() => null) as { error?: string } | null;
52 throw new Error(data?.error ?? `Revision failed (${res.status})`);
53 }
54 const data: { emailSent?: boolean } = await res.json();
55 window.location.href = `/orders/${orderId}?revised=${data.emailSent === false ? 'saved' : '1'}`;
56 } catch (e) {
57 setError(e instanceof Error ? e.message : 'Revision failed');
58 setMode('idle');
59 }
60 };
61
62 return (
63 <div className="approve__panel">
64 <div className="approve__disclosure">
65 <strong>Approving your portrait is final.</strong>
66 <p>
67 Once you approve, we send your file to our print partner. Each piece is
68 made-to-order for your pet — printed, framed, and hand-finished. Because
69 we can&rsquo;t restock or resell a custom commission,{' '}
70 <strong>all approved orders are non-refundable.</strong>
71 </p>
72 <p style={{ marginBottom: 0 }}>
73 Before this point you can request free revisions until the artwork
74 is right.
75 </p>
76 </div>
77
78 <form onSubmit={onApprove} className="approve__form">
79 <label className="approve__checkbox">
80 <input
81 type="checkbox"
82 checked={termsAccepted}
83 onChange={(e) => setTermsAccepted(e.target.checked)}
84 disabled={mode !== 'idle'}
85 />
86 <span>I understand — I&rsquo;m approving this portrait and agree the order is final.</span>
87 </label>
88
89 <button
90 type="submit"
91 className="btn btn--rose btn--lg btn--block"
92 disabled={!termsAccepted || mode !== 'idle'}
93 >
94 {mode === 'approving' ? 'Sending to studio…' : 'Approve and send to studio'}
95 </button>
96 </form>
97
98 <div className="approve__divider">— or —</div>
99
100 {!showRevise ? (
101 <button
102 type="button"
103 className="btn btn--secondary btn--md btn--block"
104 onClick={() => setShowRevise(true)}
105 disabled={mode !== 'idle'}
106 >
107 Request changes (free)
108 </button>
109 ) : (
110 <form onSubmit={onRevise} className="approve__form">
111 <label className="approve__label" htmlFor="lg-revise-note">
112 Tell us what to change
113 </label>
114 <p className="approve__hint">
115 We start with the proof you&rsquo;re reviewing. Tell us what to change
116 and what you&rsquo;d like to keep. For example, you can ask for a
117 simpler background, warmer colour, or a tighter crop. Small details
118 may shift between versions.
119 </p>
120 <textarea
121 id="lg-revise-note"
122 rows={5}
123 value={reviseNote}
124 onChange={(e) => setReviseNote(e.target.value)}
125 placeholder="e.g. warmer background, a bit brighter overall, simpler background, softer brushwork…"
126 className="auth__input"
127 style={{ fontFamily: 'inherit', minHeight: 100 }}
128 disabled={mode !== 'idle'}
129 maxLength={ORDER_MESSAGE_LIMIT}
130 required
131 />
132 <button
133 type="submit"
134 className="btn btn--rose btn--md btn--block"
135 disabled={mode !== 'idle' || !reviseNote.trim()}
136 >
137 {mode === 'revising' ? 'Sending…' : 'Send revision request'}
138 </button>
139 </form>
140 )}
141
142 {error && <p className="auth__error" style={{ marginTop: 12 }}>{error}</p>}
143 </div>
144 );
145}